package com.hotau.member;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hotau.Executable;
import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.member.service.MemberService;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;

/**
 * 登录用户，未激活，进行登录激活
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-10
 * @version 1.0
 * <ul>
 * 		<li>1.0 创建</li>
 * </ul>
 */
public class LoginActivateFragment extends Fragment implements OnClickListener {

	public static final String TAG = "LoginActivateFragment";
	private Button login_activate_btn_sendsms = null;
	private TextView activate_tv_message = null;
	private String message = null;
	
	boolean isRegister = false;
	SharedPreferences share = null;

	/**
	 * 异步请求Task
	 */
	HttpRequestAsynTask login_activate_task = null; //获取激活短信
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initAllWidget();
		initAllEvent();
		share = getActivity().getSharedPreferences("MEMBER_USER",0);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		isRegister = this.getArguments().getBoolean("isRegister");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if(isRegister){
			return inflater.inflate(R.layout.member_login_activate2, container,false);
		}else{
			return inflater.inflate(R.layout.member_login_activate, container,false);
		}
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.member_login_activate_btn_sendsms:
			//发送激活短信
			DialogUtil.showDialog(getActivity(), "", "新生支付向你手机发送了激活连接", new Executable(){
				
				@Override
				public void execute() {
					// TODO Auto-generated method stub
					//确定激活发送短信
					StringBuilder sb_smscheck = new StringBuilder();
					sb_smscheck.append("Form:userCode="+share.getString("last_login_usercode", ""));
					sb_smscheck.append(";terminalCode=" + HotauConstants.getTerminalCode(getActivity()));
					sb_smscheck.append(";type=1");
					//获取激活短信
					login_activate_task = new HttpRequestAsynTask(getActivity()){
						@Override
						protected void doPostExecute(String result) {
							// TODO Auto-generated method stub
							Log.d(TAG, "login_activate_sms_response :" + result.toString());
							try {
								JSONObject data = new JSONObject(result);
								if(data != null && data.getInt("result") == 1){
									//成功
									DialogUtil.showDialog(getActivity(), "", "激活校验码发送成功,确定重新登录",new Executable() {
										@Override
										public void execute() {
											// TODO Auto-generated method stub
											HotauActivity csmbpActivity = (HotauActivity) getActivity();
											MemberService.logout(csmbpActivity);
											csmbpActivity.pushFragment("登录",new LoginFragment());
										}
									});
//									login_activate_btn_sendsms.setBackgroundResource(R.drawable.member_login_activat_btn_bg_wait);
//									login_activate_btn_sendsms.setEnabled(false);
//									handler.postDelayed(new Runnable() {
//										@Override
//										public void run() {
//											login_activate_btn_sendsms.setBackgroundResource(R.drawable.member_login_activat_btn_bg);
//											login_activate_btn_sendsms.setEnabled(true);
//											
//										}
//									}, 60*1000);
								}else{
									//失败
									DialogUtil.showDialog(getActivity(), "", data.getString("errorMsg"));
								}
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					};
					login_activate_task.execute(HotauConstants.MEMBER_GET_CHECK_CODE_URL,sb_smscheck.toString(),HttpUtil.UTF8_ENCODING);
					Log.d(TAG, "login_activate_sms_request :" + sb_smscheck.toString());
				}
				
			}, new Executable(){

				@Override
				public void execute() {
					// TODO Auto-generated method stub
					//取消
					//什么都不错
				}
				
			});
			break;

		default:
			break;
		}
	}
	
	/**
	 * 初始化所有控件
	 */
	public void initAllWidget() {
		login_activate_btn_sendsms = (Button) getActivity().findViewById(R.id.member_login_activate_btn_sendsms);
//		activate_tv_message = (TextView) getActivity().findViewById(R.id.member_login_activate_tv_message);
//		activate_tv_message.setText(message);
		
	}
	
	/**
	 * 初始化所有事件
	 */
	public void initAllEvent() {
		login_activate_btn_sendsms.setOnClickListener(this);
	}
	
	public Handler handler = new Handler();

}
